﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;


namespace wpNewUsers1.wpNewUsers
{
    [ToolboxItemAttribute(false)]
    public class wpNewUsers : WebPart
    {
        int myCountDay = 3;

        [WebBrowsable(true), WebDisplayName("Кол-во дней"), WebDescription("Кол-во дней назад."),
        Personalizable(PersonalizationScope.Shared), Category("(Ko) Settings")]
        public int CountDay
        {
            get { return myCountDay; }
            set { myCountDay = value; }
        }

        DateTime dDate1;
        public List<string> mMonthName = new List<string>() { "Января", "Февраля", "Марта", "Апреля", "Мая", "Июня", "Июля", "Августа", "Сентября", "	Октября", "Ноября", "Декабря" };
        DateTime d2 = DateTime.Now.AddDays(1).Date;

        string sURL = "http://sp.korablik.local";
        List<dsData> dsProperty = new List<dsData>();

        protected override void CreateChildControls()
        {
            //--
            dDate1 = DateTime.Now.AddDays(-30);
            //--
            SPSite site = new SPSite(sURL);

            SPWeb spUWeb = site.OpenWeb();
            //spUWeb.Users.q
            SPServiceContext context = SPServiceContext.GetContext(site);
            UserProfileManager uprofileManager = new UserProfileManager(context);
           // ProfileSearchManager profileSM = uprofileManager.;

            
            
            IEnumerator profileEnumerator = uprofileManager.GetEnumerator();

            //SPList spUList = profileEnumerator as SPList;
            
            //SPListItemCollection listcollection = spUList.GetItems(new SPQuery()
            //{
            //    Query = "<Where><IsNotNull><FieldRef Name='SPS-HireDate' /></IsNotNull></Where>",
            //    ViewFields = "<FieldRef Name='SPS-HireDate' />"
            //});

            

            //Label lbl = new Label();
            //lbl.Text = listcollection[0]["SPS-HireDate"].ToString();
            //Controls.Add(lbl);
            //while (profileEnumerator.MoveNext())
            //{
            //    UserProfile currentProfile = profileEnumerator.Current as UserProfile;

            //    if (currentProfile["SPS-HireDate"].Value != null)
            //    {
            //        DateTime d3 = DateTime.Parse(currentProfile["SPS-HireDate"].Value.ToString());
            //        d3 = d3.Date;
            //        //Label lbl = new Label();
            //        //lbl.Text = d3 +">="+ dDate1+"<br>";
            //        //Controls.Add(lbl);
            //        if (d3 >= dDate1)
            //        {
            //            dsData dRow = new dsData();
            //            dRow.HireDate = d3;
            //            dRow.FullName = currentProfile["PreferredName"].Value.ToString();
            //            //--
            //            if (currentProfile["SPS-JobTitle"].Value != null)
            //                dRow.Staff = currentProfile["SPS-JobTitle"].Value.ToString();
            //            if (currentProfile["Department"].Value != null)
            //                dRow.Department = currentProfile["Department"].Value.ToString();
            //            if (currentProfile["Office"].Value != null)
            //                dRow.Office = currentProfile["Office"].Value.ToString();
            //            if (currentProfile["PictureUrl"].Value != null)
            //                dRow.ImageUrl = currentProfile["PictureUrl"].Value.ToString();
            //            if (currentProfile.PersonalSite != null)
            //                dRow.SiteUrl = currentProfile.PersonalSite.Url.ToString();

            //            dsProperty.Add(dRow);
            //        }
            //    }
            //}
                dsProperty.Sort((x, y) => -x.HireDate.CompareTo(y.HireDate)); //    DateTime.Compare(x.Birthday, y.Birthday));
                //--
                Table tb = new Table();
                TableRow tRow;
                TableCell tCell;
                foreach (dsData row in dsProperty)
                {
                    tRow = new TableRow();
                    tRow.BorderWidth = 0;
                    //--
                    tCell = new TableCell();
                    tCell.Width = 90;

                    Image im = new Image();
                    im.BorderWidth = 1;
                    im.Width = 80;
                    im.ImageUrl = row.ImageUrl;

                    tCell.Controls.Add(im);
                    tRow.Cells.Add(tCell);
                    //--
                    tCell = new TableCell();
                    tCell.Width = 200;
                    tCell.VerticalAlign = VerticalAlign.Top;
                    tCell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
                    //--
                    Label l2 = new Label();
                    l2.Text = row.HireDate.Day.ToString() + " " + mMonthName[row.HireDate.Month - 1] + "<br>";
                    l2.Font.Size = 10;
                    l2.Font.Bold = true;
                    l2.ForeColor = System.Drawing.Color.Blue;
                    tCell.Controls.Add(l2);
                    //--
                    if (row.SiteUrl != null)
                    {
                        HyperLink hl1 = new HyperLink();
                        hl1.Text = row.FullName + "<br>";
                        hl1.Font.Size = 8;
                        hl1.NavigateUrl = row.SiteUrl;
                        tCell.Controls.Add(hl1);
                    }
                    else
                    {
                        Label l1 = new Label();
                        l1.Text = row.FullName + "<br>";
                        l1.Font.Size = 8;
                        tCell.Controls.Add(l1);
                    }
                    //--
                    if (row.Department != null)
                    {
                        Label l4 = new Label();
                        l4.Text = row.Department + "<br>";
                        l4.Font.Size = 9;
                        tCell.Controls.Add(l4);
                    }
                    //--
                    if (row.Staff != null)
                    {
                        Label l5 = new Label();
                        l5.Text = row.Staff + "<br>";
                        l5.Font.Size = 9;
                        tCell.Controls.Add(l5);
                    }
                    //--
                    if (row.Office != null)
                    {
                        Label l3 = new Label();
                        l3.Text = row.Office;
                        l3.Font.Size = 9;
                        tCell.Controls.Add(l3);
                    }
                    //--
                    tRow.Cells.Add(tCell);
                    //--
                    tb.Rows.Add(tRow);
                }
                this.Controls.Add(tb);
                //--
            }
        }
    }
